<?php

namespace app\api\service\Activity;

use addons\xyb\XypHelper;
use app\common\model\daily\UsersModel;
use app\common\model\marketing\Association;
use app\common\model\marketing\AssociationJobs;

use app\common\model\marketing\AssociationProgress;
use app\common\model\marketing\label\Label;
use app\common\model\marketing\label\Level;
use app\common\model\marketing\MarketingCoupons;
use app\common\model\marketing\UsersMarketingCoupons;




class AssociationJobsService
{

    public static function distributeJobsRewards($user_id, $jobs_id)
    {
        $field = 'id,reward_method,reward_point,youth_point,reward_coupons_id';
        $jobs = AssociationJobs::field($field)->where('id', $jobs_id)->find();
        if(in_array($jobs->reward_method, [1, 4])){
            UsersModel::increaseUserCurrency($user_id, $jobs->reward_point, 'score');
        }
        if(in_array($jobs->reward_method, [2, 5])){
            UsersModel::increaseUserCurrency($user_id, $jobs->youth_point, 'youth_coin');
        }
//        if(in_array($jobs->reward_method, [3, 4, 5])){
//            if(!$reward_coupons_id = XypHelper::formatJsonStringToArray($jobs->reward_coupons_id)){
//                return false;
//            }
//            foreach ($reward_coupons_id as $value){
//                $field = 'id,expiration_method,use_end_time,effective_days,type';
//                $coupon = MarketingCoupons::field($field)->where('id', $value['id'])->find();
//                UsersMarketingCoupons::usersJobsReceiveCoupons($user_id, $coupon);
//            }
//        }
        return true;
    }

    public static function distributeJobsCouponsRewards($user_id, $progress_id)
    {
        $field = 'id,examine_stated,receive_stated,jobs_id';
        $progress = AssociationProgress::field($field)->where('id', $progress_id)->find();
        if(!$progress){
            return false;
        }
//        if(!in_array($progress->examine_stated, [2, 4])){
//            return false;
//        }
//        if($progress->receive_stated == 1){
//            return false;
//        }
        $jobs = AssociationJobs::field('id,reward_method,reward_point,youth_point,reward_coupons_id')->where('id', $progress->jobs_id)->find();
        if(!$jobs){
            return false;
        }

        if(in_array($jobs->reward_method, [3, 4, 5])){
            if(!$reward_coupons_id = XypHelper::formatJsonStringToArray($jobs->reward_coupons_id)){
                return false;
            }
            foreach ($reward_coupons_id as $value){
                $field = 'id,expiration_method,use_end_time,effective_days,type';
                $coupon = MarketingCoupons::field($field)->where('id', $value['id'])->find();
                $res = UsersMarketingCoupons::usersJobsReceiveCoupons($user_id, $coupon,2);
            }
        }
        $progress->reward_stated = 1;
        $progress->receive_stated = 1;
        $progress->reward_time = date('Y-m-d H:i:s');
        $progress->receive_time = date('Y-m-d H:i:s');
        $progress->save();
        return true;
    }

    public static function distributeJobsLabels($user_id, $jobs_id){
        $field = 'id,reward_method,reward_point,youth_point,reward_coupons_id';
        if(!$jobs = AssociationJobs::where('id',$jobs_id)->find()){
            return false;
        }

        $field = 'id,label_id,category_id';
        $association = Association::field($field)->where('id', $jobs->association_id)->find();
        if(!$association || !$association->label_id){
            return false;
        }
        $field = 'id,name,category_id';
        $label = Label::field($field)->where('id', $association->label_id)->find();
        if(!$label){
            return false;
        }
        if(!$category_ids = XypHelper::stringToArray($label->category_id)){
            return false;
        }
       // var_dump($category_ids);
        $insert = [
            'user_id'       => $user_id,
            'parent_id'     => isset($category_ids[0])?$category_ids[0]:0,
            'category_id'   => isset($category_ids[1])?$category_ids[1]:0,
            'label_id'      => $association->label_id,
            'label_name'    => $label->name,
            'level_id'   => 0,
            'level_youth'   => 0,
            'level_coupons' => 0,
            'level_status' => 1,
        ];
    }

}